<?php

namespace app\common\command\modelcreater;


class TableModelMaker extends Base
{
    public string $tableName = "";
    public string $tableRemark = "";
    public bool $isSoftDelete = false;
    public string $softDeleteColumn = "";

    public array $columns = [];

    public string $endOfLine = "\r\n";


    protected function getTypeName($type): string
    {
        //只保留括号前面的内容
        $type = preg_replace('/\(.*/', '', $type);
        return match ($type) {
            "int", "tinyint", "smallint", "mediumint", "bigint", "timestamp" => "int",
            "float", "double", "decimal", "bit" => "float",
            "char", "varchar", "binary", "varbinary", "tinytext", "text", "mediumtext", "longtext", "enum", "set" => "string",
            "tinyblob", "blob", "mediumblob", "longblob", "date", "datetime", "json" => "mixed",
            "time" => "time",
            "year" => "year",
            "point" => "point",
            "linestring" => "linestring",
            "polygon" => "polygon",
            "multipoint" => "multipoint",
            "multilinestring" => "multilinestring",
            "multipolygon" => "multipolygon",
            default => $type,
        };

    }

    private function formatString($str, $len = 8)
    {
        //格式化字符串，不足长度补空格
        while (strlen($str) < $len) {
            $str = $str . " ";
        }
        return $str;
    }


    /**
     * 获取模型的代码
     * @return string
     */
    public function getCode(): string
    {
        $codeStr = "<?php" . $this->endOfLine;
        $codeStr .= "namespace app\common\model\base;" . $this->endOfLine;
        $codeStr .= "use app\BaseModel;" . $this->endOfLine;
        if ($this->isSoftDelete) {
            $codeStr .= "use think\model\concern\SoftDelete;" . $this->endOfLine;
        }
        $codeStr .= $this->endOfLine;
        $codeStr .= "/**" . $this->endOfLine;
        //设置注解
        foreach ($this->columns as $column) {
            $comment = str_replace("\n", "", $column['Comment']);
            $comment = str_replace("\r", "", $comment);
            $codeStr .= " * @property " . $this->formatString($this->getTypeName($column['Type']), 10) . " \$" . $this->formatString($column['Field'], 20) . " " . $comment . $this->endOfLine;
        }
        $codeStr .= ' * @method   self       find($data = null)     找单条记录' . $this->endOfLine;
        $codeStr .= " */" . $this->endOfLine;
        $codeStr .= "class " . $this->tableName . " extends BaseModel" . $this->endOfLine;
        $codeStr .= "{" . $this->endOfLine;
        if ($this->isSoftDelete) {
            $codeStr .= "    use SoftDelete;" . $this->endOfLine;
        }
        //设置字段
        $codeStr .= "    protected \$schema = [" . $this->endOfLine;
        foreach ($this->columns as $column) {
            $keyStr = "'" . $column['Field'] . "'";
            $typeStr = "'" . $this->getTypeName($column['Type']) . "'";
            $comment = str_replace("\n", "", $column['Comment']);
            $comment = str_replace("\r", "", $comment);
            $codeStr .= "        " . $this->formatString($keyStr, 20) . " => " . $this->formatString($typeStr, 10) . ", //" .$comment. $this->endOfLine;
        }
        $codeStr .= "    ];" . $this->endOfLine;
        $codeStr .= "}" . $this->endOfLine;

        return $codeStr;
    }


    /**
     * 获取文件路径
     * @return string
     */
    public function getFilePath(): string
    {
        return root_path() . "/app/common/model/base/" . $this->getFileName();
    }

    /**
     * 获取文件名
     * @return string
     */
    public function getFileName(): string
    {
        return $this->tableName . ".php";
    }

}